package mysql;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
 * @author ：ALi
 * @date ：Created in 2021/12/9 14:42
 * @description：数据库数据导入
 * @modified By：
 * @version: $
 */
public class DataPre {
    public static void main(String[] agrs){
        Set<String[]> set = new HashSet<>(50000);

        File file = new File("F:\\test3.txt");
        txt2String(file,set);
        Iterator<String[]> iterator = set.iterator();

        System.out.println("sum:"+set.size());
        //得到一行的数据
        while (iterator.hasNext()){
            String[] next = iterator.next();
            String sql = "insert into se_customer(customer_type,potential,customer_name,legal_person,establish_date,social_credit_code,potential_type) values(1,1"+",'"+next[0]+"','"+next[2]+"','"+next[3]+"','"+next[1]+"',"+"'校友会'"+")";
            db(sql);
//            System.out.println(Arrays.toString(iterator.next()));
        }

//        db("2");





    }
    public static void db(String str) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            // 加载驱动类
            Class.forName("com.mysql.jdbc.Driver");
            long start = System.currentTimeMillis();

            // 建立连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/assist?useUnicode=true&characterEncoding=utf-8&useSSL=false",
                    "root", "root");
            long end = System.currentTimeMillis();


            // 创建Statement对象
            stmt = conn.createStatement();

            // 执行SQL语句
//            stmt.execute("insert into se_customer(customer_type,potential,customer_name,legal_person,establish_date,social_credit_code,potential_type) values(1,1,'雷波溪洛渡商会','陈治彬','2006-09-14','51513437MJR493309U','商会')");
//            stmt.execute("insert into se_customer(customer_type,potential,customer_name,legal_person,establish_date,social_credit_code,potential_type) values(1,1,'何建宏',"成都酒业商会"+"2013-01-31"+"515101000624147002"+"商会"+")");
            stmt.execute(str);
//            rs = stmt.executeQuery("select * from se_customer");
////            System.out.println("id\tname\tage\tsex");
//            while (rs.next()) {
////                System.out.println(rs.getInt(1) + "\t" + rs.getString(2)
////                        + "\t" + rs.getInt(3) + "\t" + rs.getInt(4)+ "\t" + rs.getInt(5) + "\t" + rs.getInt(6)+ "\t" + rs.getInt(7) + "\t");
//            System.out.println();
//            }


        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }

            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public static Set txt2String(File file,Set set){
        StringBuilder result = new StringBuilder();
        try{
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            while((s = br.readLine())!=null){//使用readLine方法，一次读一行
                String[] res = new String[4];
                int index = s.indexOf("|");
                String name = s.substring(0,index).trim();

                int index1 = s.indexOf(":");
                String code = s.substring(index1+2,index1+20).trim();

                int index2 = s.indexOf(":",(index1+20));
                int index22 = s.lastIndexOf("成");
                String person = s.substring(index2+1,index22).trim();

                int index3 = s.lastIndexOf(":");
                String date = s.substring(index3+1).trim();
                res[0] = name;
                res[1] = code;
                res[2] = person;
                res[3] = date;
                set.add(res);

            }
            br.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return set;
    }

}